/*
 * Copyright (C) 2016 huanghaibin_dev <huanghaibin_dev@163.com>
 * WebSite https://github.com/MiracleTimes-Dev
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.talkweb.calendarview;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;


/**
 * 年份+月份选择布局
 * ViewPager + RecyclerView
 */
public final class YearViewPager extends BaseViewPage {
    private int mYearCount;
    private boolean isUpdateYearView;
    private CalendarViewDelegate mDelegate;
    private YearRecyclerView.OnMonthSelectedListener mListener;

    public YearViewPager(Context context) {
        this(context, null);
    }

    public YearViewPager(Context context, AttrSet attrs) {
        super(context, attrs);
    }


    void setup(CalendarViewDelegate delegate) {
        this.mDelegate = delegate;
        this.mYearCount = mDelegate.getMaxYear() - mDelegate.getMinYear() + 1;
        setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return mYearCount;
            }

            @Override
            public int getPageIndex(Object object) {
                return isUpdateYearView ? POSITION_INVALID : super.getPageIndex(object);
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int position) {
                YearRecyclerView view = new YearRecyclerView(getContext());
                componentContainer.addComponent(view, LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
                view.setup(mDelegate);
                view.setOnMonthSelectedListener(mListener);
                view.init(position + mDelegate.getMinYear());

                return view;
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object object) {
                componentContainer.removeComponent((Component) object);
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object object) {
                return component == object;
            }
        });

        setCurrentPage(mDelegate.getCurrentDay().getYear() - mDelegate.getMinYear());
    }


    @Override
    public void setCurrentPage(int item) {
        setCurrentPage(item, false);
    }

    @Override
    public void setCurrentPage(int item, boolean smoothScroll) {
        if (Math.abs(getCurrentPage() - item) > 1) {
            super.setCurrentPage(item, false);
        } else {
            super.setCurrentPage(item, false);
        }
    }

    /**
     * 通知刷新
     */
    void notifyDataSetChanged() {
        this.mYearCount = mDelegate.getMaxYear() - mDelegate.getMinYear() + 1;
        if (getProvider() != null) {
            getProvider().notifyDataChanged();
        }

    }

    /**
     * 滚动到某年
     *
     * @param year         year
     * @param smoothScroll smoothScroll
     */
    void scrollToYear(int year, boolean smoothScroll) {
        setCurrentPage(year - mDelegate.getMinYear(), smoothScroll);
    }

    /**
     * 更新日期范围
     */
    final void updateRange() {
        isUpdateYearView = true;
        notifyDataSetChanged();
        isUpdateYearView = false;
    }

    /**
     * 更新界面
     */
    final void update() {
        for (int i = 0; i < getChildCount(); i++) {
            YearRecyclerView view = (YearRecyclerView) getComponentAt(i);
            view.notifyAdapterDataSetChanged();
        }
    }


    /**
     * 更新周起始
     */
    final void updateWeekStart() {
        for (int i = 0; i < getChildCount(); i++) {
            YearRecyclerView view = (YearRecyclerView) getComponentAt(i);
            view.updateWeekStart();
            view.notifyAdapterDataSetChanged();
        }
    }

    /**
     * 更新字体颜色大小
     */
    final void updateStyle() {
        for (int i = 0; i < getChildCount(); i++) {
            YearRecyclerView view = (YearRecyclerView) getComponentAt(i);
            view.updateStyle();
        }
    }


    final void setOnMonthSelectedListener(YearRecyclerView.OnMonthSelectedListener listener) {
        this.mListener = listener;
    }


//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        //heightMeasureSpec = MeasureSpec.makeMeasureSpec(getHeight(getContext(), this), MeasureSpec.EXACTLY);
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//    }

//    /**
//     * 计算相对高度
//     *
//     * @param context context
//     * @param view    view
//     * @return 年月视图选择器最适合的高度
//     */
//    private static int getHeight(Context context, View view) {
//        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//        assert manager != null;
//        Display display = manager.getDefaultDisplay();
//        int h = display.getHeight();
//        int[] location = new int[2];
//        view.getLocationInWindow(location);
//        view.getLocationOnScreen(location);
//        return h - location[1];
//    }
//
//    @SuppressLint("ClickableViewAccessibility")
//    @Override
//    public boolean onTouchEvent(MotionEvent ev) {
//        return mDelegate.isYearViewScrollable() && super.onTouchEvent(ev);
//    }
//
//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        return mDelegate.isYearViewScrollable() && super.onInterceptTouchEvent(ev);
//    }
}
